/*
 * Copyright (C) Antonio Ramon Morcillo
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package soft.xitxarello.su.adapters;

import java.util.List;

import soft.xitxarello.su.R;
import soft.xitxarello.su.data.App;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * A concrete BaseAdapter that is backed by an array of uninstallable
 * applications.
 * 
 * @see android.widget.ArrayAdapter
 */
public class AppListAdapter extends ArrayAdapter<App> {

    // context
    private final Context context;

    // inflater.
    private final LayoutInflater mInflater;

    /**
     * Default constructor.
     * 
     * @param context Interface to global information about an application
     *            environment.
     */
    public AppListAdapter(Context context) {
        super(context, android.R.layout.simple_list_item_2);
        this.context = context;
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    /**
     * Specifies the data from scratch for the adapter.
     * 
     * @param data list of uninstallable applications.
     */
    public void setData(List<App> data) {
        clear();
        if (data != null) {
            for (App app : data) {
                this.add(app);
            }
        }
    }

    /**
     * Overrides a superclass method. Represents the basic building block for
     * user interface components, in ArrayAdapter for more view's than TextView
     * we have to override getView method.
     * 
     * @param position Index (starting at 0) of the data item to be selected.
     * @param convertView The old view to reuse, if possible.
     * @param parent ViewGroup where this view is in.
     * @return a representation of the list as a view.
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        // A ViewHolder keeps references to children views to avoid unneccessary
        // calls to findViewById() on each row.
        ViewHolder holder;

        // When convertView is not null, we can reuse it directly, there is no
        // need to reinflate it. We only inflate a new View when the convertView
        // supplied by ListView is null.
        if (convertView == null) {
            // Reinflate.
            convertView = mInflater.inflate(R.layout.list_item, parent, false);
            // Creates a ViewHolder and store references to the three children
            // views we want to bind data to.
            holder = new ViewHolder();
            holder.name = (TextView) convertView.findViewById(R.id.app_name);
            holder.version = (TextView) convertView.findViewById(R.id.app_version);
            holder.icon = (ImageView) convertView.findViewById(R.id.app_icon);
            convertView.setTag(holder);
        } else {
            // Get the ViewHolder back to get fast access to the TextViews
            // and the ImageView.
            holder = (ViewHolder) convertView.getTag();
        }

        // Get values from App object.
        App item = getItem(position);

        // Bind the data efficiently with the holder.
        holder.icon.setImageDrawable(item.getIcon(this.context));
        holder.name.setText(item.getName(this.context));
        holder.version.setText(context
                .getString(R.string.app_version) + " " + item.getVersion(this.context));

        // At last...
        return convertView;
    }

    /**
     * Simply nested class into the adapter that we're using for the ListViews
     * rows.
     */
    static class ViewHolder {
        TextView name;
        TextView version;
        ImageView icon;
    }
}
